package com.org.web.handle.car.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.org.platform.model.ResultModel;
import com.org.platform.utils.CommonUtil;
import com.org.platform.utils.LocalDateUtil;
import com.org.web.handle.car.dao.CarMapper;
import com.org.web.handle.user.service.UserService;
import com.org.web.handle.vo.CarVo;
import com.org.web.handle.vo.UserVo;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author devg
 * @Date 2020/3/2 17:05
 */

@Slf4j
@Service
public class CarService {

  @Autowired
  private StringRedisTemplate stringRedisTemplate;

  @Autowired
  private UserService userService;

  @Autowired
  private CarMapper carMapper;

  public ResultModel addToCar(CarVo carVo) {
    Long prodId = carVo.getProdId();
    Integer prodNum = carVo.getProdNum();
    if (CommonUtil.isNullOrEmpty(prodId)) {
      return ResultModel.newFail("商品信息错误");
    }
    String addTime = LocalDateUtil.localDateTimeFormat(LocalDateTime.now(), LocalDateUtil.FORMAT_PATTERN2);
    carVo.setAddTime(addTime);
    UserVo user = userService.getUser();
    String loginName = user.getLoginName();
    String prodKey = prodId.toString();
    String prodJson = JSONObject.toJSONString(carVo);
    log.info("user:{} add prod prodId:{} prodNum:{} to car", loginName, prodKey, prodNum);
    stringRedisTemplate.opsForHash().put(loginName, prodKey, prodJson);
    return ResultModel.newSuccess();
  }

  public ResultModel deleteCar(List<String> deleteIds) {
    UserVo user = userService.getUser();
    String loginName = user.getLoginName();
    stringRedisTemplate.opsForHash().delete(loginName, deleteIds.toArray());
    return ResultModel.newSuccess();
  }

  /**
   * 获取购物车数据
   *
   * @return
   */
  public List<CarVo> getCarList() {
    List<CarVo> resultList = new ArrayList<>();
    UserVo user = userService.getUser();
    String loginName = user.getLoginName();
    Set<Object> keys = stringRedisTemplate.opsForHash().keys(loginName);
    List<Object> list = stringRedisTemplate.opsForHash().multiGet(loginName, keys);
    if (!CommonUtil.isNullOrEmpty(list)) {
      for (Object object : list) {
        CarVo jsonObject = JSON.parseObject(object.toString(), CarVo.class);
        resultList.add(jsonObject);
      }
      List<CarVo> carList = carMapper.getCarList(resultList);
      Label:
      for (CarVo carVo : resultList) {
//         填充购物车数据
        Long prodId = carVo.getProdId();
        if (!CommonUtil.isNullOrEmpty(carList)) {
          for (CarVo car : carList) {
            Long tempProdId = car.getProdId();
            if (CommonUtil.eq(tempProdId, prodId)) {
              BeanUtils.copyProperties(car, carVo, "prodNum", "addTime");
              continue Label;
            }
          }
        }
      }
    }
    if (!CommonUtil.isNullOrEmpty(resultList)) {
      // 按加入时间排序
      Collections.sort(resultList, new Comparator<CarVo>() {
        @Override
        public int compare(CarVo o1, CarVo o2) {
          String addTime1 = o1.getAddTime();
          String addTime2 = o2.getAddTime();
          return addTime2.compareTo(addTime1);
        }
      });
    }
    return resultList;
  }
}
